<?php	if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
 * 分享验证
 * @author LM
 * @modifier: Skiychan <dev@skiy.net>
 */
class Share_check extends CI_Controller{
	
	public function __construct() {
		parent::__construct ();
        $this->load->service('share_page_service');
	}

    /**
     * 密码输入页
     */
	public function index(){
		//生成唯一标识符
		$share_uniqid = sha1(uniqid(mt_rand(), true));
		$this->session->set_userdata('share_uniqid', $share_uniqid);

		//移动端转跳
		$this->load->library('user_agent');
		$share_code = $this->input->get('code');
		if ($this->agent->is_mobile()) {
			$platform = $this->input->get("platform");
			$refresh = time();

			$local_url = sprintf("/m/download.html?_=%s#index/index?code=%s&platform=%s",
				$refresh,
				$share_code,
				$platform);
			return redirect($local_url);
		}

		$share_res = $this->share_page_service->get_share_link($share_code);
        //无code跳转至首页
        if (empty($share_code) || empty($share_res)) redirect('/');

		$data = array(
				'share_code' => $share_code,
				'share_uniqid'     => $share_uniqid,
				'title'=>$share_res['name']
				);
		$this->template->view('share/check.tpl', $data);
	}

    /**
     * 验证密码页
     */
	public function check(){
		$pwd = $this->input->post('pwd', TRUE);
		$share_code = $this->input->post('share_code', TRUE);
		$share_uniqid = $this->input->post('share_uniqid');

		if (empty($pwd) || empty($share_code) || empty($share_uniqid)) {
			return Common::ajax_return(1003);
		}

		if ($share_uniqid != $this->session->userdata('share_uniqid')) {
			return Common::ajax_return(1003);
		}

        $share_res = $this->share_page_service->get_share_link($share_code);
		if ($pwd == $share_res['password']) {
			$data = array(
				'share_check' => $share_code
			);
			$this->session->set_userdata($data);
			return Common::ajax_return(0);
		}

		return Common::ajax_return(7105);
	}
	
	
}